java开发之SpringBoot转发和重定向 您所在的位置:网站首页 spring boot重定向 java开发之SpringBoot转发和重定向

java开发之SpringBoot转发和重定向

2024-07-03 21:51| 来源: 网络整理| 查看: 265

1、本文内容 3 个知识点 SpringMVC 中转发如何实现?SpringMVC 重定向如何实现?重定向 3 种传参方式 2、转发 2.1、servlet 原生实现转发 request.getRequestDispatcher(path).forward(request,response); 2.2、SpringMVC 实现转发

接口需满足下面这 3 条的会被 SpringMVC 当做转发进行处理java培训

接口返回值为 String 类型返回值格式:forward:转发的路径方法或者类上不要标注@ResponseBody 注解

案例代码如下,当访问/forward/test1的时候,返回值以forward:开头,SpringMVC 会将请求转发到/forward/test2

@RequestMapping("/forward/test1") public String test1() { return "forward:/forward/test2"; } @RequestMapping(value = "/forward/test2", produces = "text/html;charset=UTF-8") @ResponseBody public String test2() { return "我是test2"; }

测试效果:浏览器中访问/forward/test1输出

 

3、重定向 3.1、servlet 原生实现重定向 response.sendRedirect(url); 3.2、SpringMVC 实现重定向

接口需满足下面这 3 条的会被 SpringMVC 当做转发进行处理

接口返回值为 String 类型返回值格式:redirect:重定向的路径方法或者类上不要标注@ResponseBody 注解

案例代码如下,当访问/redirect/test1的时候,返回值以redirect:开头,SpringMVC 会将请求转发到/redirect/test2

@RequestMapping("/redirect/test1") public String test1() { return "redirect:/redirect/test2"; } @RequestMapping(value = "/redirect/test2", produces = "text/html;charset=UTF-8") @ResponseBody public String test2() { return "我是test2"; }

测试效果:浏览器中访问/redirect/test1,效果如下,浏览器地址栏变成/redirect/test2了

 

3.3、重定向传参方式 1:手动在地址后拼接参数

直接在重定向的 url 中直接拼接参数,如redirect:地址?name=路人&age=30,如

@RequestMapping("/redirect/test1") public String test1() { return "redirect:/redirect/test2?name=路人&age=30"; } 3.4、重定向传参方式 2:RedirectAttributes.addAttribute("参数","值") 用法 接口中需要有一个类型为RedirectAttributes的参数调用redirectAttributes.addAttribute("参数","值"),放入重定向需要传递的参数**原理:**通过redirectAttributes.addAttribute丢进去的参数,SpringMVC 重定向的时候,会自动将这些参数以?参数1=值1&参数2=值2拼接到重定向的地址上,类似于上面的方式 1。 案例代码

访问接口 test3,会被重定向到 test4,顺便传递了 2 个参数

@RequestMapping("/redirect/test3") public String test3(RedirectAttributes redirectAttributes) { redirectAttributes.addAttribute("name", "路人"); redirectAttributes.addAttribute("age", 30); return "redirect:/redirect/test4"; } @RequestMapping(value = "/redirect/test4", produces = MediaType.APPLICATION_JSON_VALUE) @ResponseBody public Map test4(@RequestParam("name") String name, @RequestParam("age") int age) { Map result = new LinkedHashMap(); result.put("name", name); result.put("age", age); return result; } 测试效果

浏览器中访问/redirect/test3接口,会被重定向到/redirect/test4,效果如下,test3 方法中丢到addAttribute中的 2 个参数name和age,被自动拼接到地址后面了。

 

3.5、重定向传参方式 2:RedirectAttributes.addFlashAttribute("参数","值")

上面我们使用的是RedirectAttributes的addAttribute放入参数,java培训机构这次我们要使用另外一个方法addFlashAttribute放入重定向需要传递的参数,具体有什么区别呢,请向下看。

用法 接口中需要有一个类型为RedirectAttributes的参数调用redirectAttributes.addFlashAttribute("参数","值"),这种方式传递的参数是被隐藏的,不会被拼接在地址后,内部是通过 session 共享数据来实现的。被重定向到的接口,需要使用一个org.springframework.ui.Model或者org.springframework.ui.ModelMap类型的参数来接收传递过来的参数,调用model.getAttribute("参数名")可以获取传递过来的参数 案例代码

访问接口 test5,会被重定向到 test6,顺便传递了 2 个参数

@RequestMapping("/redirect/test5") public String test5(RedirectAttributes redirectAttributes) { redirectAttributes.addFlashAttribute("name", "路人"); redirectAttributes.addFlashAttribute("age", 30); return "redirect:/redirect/test6"; } /** * 需要使用一个org.springframework.ui.Model或者org.springframework.ui.ModelMap类型的参数来接收传递过来的参数, * 方法内部调用model.getAttribute("参数名")可以获取传递过来的参数 * * @param model * @return */ @RequestMapping(value = "/redirect/test6", produces = MediaType.APPLICATION_JSON_VALUE) @ResponseBody public Map test6(Model model) { String name = (String) model.getAttribute("name"); Integer age = (Integer) model.getAttribute("age"); Map result = new LinkedHashMap(); result.put("name", name); result.put("age", age); return result; } 测试效果

浏览器中访问/redirect/test5接口,会被重定向到/redirect/test6,效果如下,参数传递成功了,传递是隐藏式的。

 

原理

redirectAttributes.addFlashAttribute 放入重定向需要传递的参数,SpringMVC 在重定向到新地址之前,会将这部分数据丢到 session 中,当重定向的请求过来后,SpringMVC 又会从 session 中拿到这部分数据,然后丢到 Model 或者 ModelMap 中,然后冲 session 中清理掉这部分数据。

3.6、RedirectAttributes.addAttribute 和 RedirectAttributes.addFlashAttribute 区别 都可以实现重定向传递参数addAttribute 传递的参数,最后会追加在新的地址上,而 addFlashAttribute 传递的参数是隐藏式的addFlashAttribute 可以传递大量的信息,不过 addFlashAttribute 有个弊端,重定向到新地址之后,如下图,如果此时用户刷新页面,传递的参数取不到了,就丢失了,建议使用方式 1 和方式 2;方式 3 可以作为了解。

 

4、案例代码 git 地址 4.1、git 地址 https://gitee.com/javacode2018/springmvc-series 4.2、本文案例代码结构说明

 

文章来源:路人甲Java



【本文地址】

公司简介

联系我们

今日新闻

    推荐新闻

    专题文章
      CopyRight 2018-2019 实验室设备网 版权所有